package com.ybkj.o2o.zhuanxian.common;

import com.ybkj.o2o.zhuanxian.common.config.DaijiaApi;
import com.ybkj.o2o.zhuanxian.entity.Order;
import com.ybkj.o2o.zhuanxian.manager.OrderManager;
import com.ybkj.o2o.zhuanxian.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * 随机红包
 */
@Component
public class RandomRedPacket {

    @Autowired
    private DaijiaApi daijiaApi;
    @Autowired
    private OrderService orderService;

    /**
     * [0.1,1.0]的随机红包
     * 概率：80%
     */
    private static Double[] sectionOne = new Double[]{0.1,1.0};

    /**
     * [1.1,2.0]的随机红包
     * 概率：14%
     */
    private static Double[] sectionTwo = new Double[]{1.1,2.0};

    /**
     * [2.1,3.0]的随机红包
     * 概率：3%
     */
    private static Double[] sectionThree = new Double[]{2.1,3.0};

    /**
     * [3.1,4.0]的随机红包
     * 概率：2%
     */
    private static Double[] sectionFour = new Double[]{3.1,4.0};

    /**
     * [4.1,5.0]的随机红包
     * 概率：1%
     */
    private static Double[] sectionFive = new Double[]{4.1,5.0};

    /**
     * 周期cron表达式
     */
    public static final String CRON_WEEK = "0 0 0 ? * SUN";
    public static final String CRON_MONTH = "0 0 0 1 * ?";

    /**
     * 红包生成周期
     */
    public static String CYCLE_MONTH = "month";
    public static String CYCLE_WEEK = "week";

    /**
     * 判断是否生成最大红包
     */
//    public Map<String,Object> isBiggestRedPacket(Date orderDate){
//        boolean isBiggestRedPacket = false;
//        System.out.println("daijia:"+daijiaApi);
//        System.out.println("orderManager:" + orderService);
//        //获取周期类型
//        String cycle = daijiaApi.isRandomRedPacket("cycle").toString();
//        System.out.println("cycle:"+cycle);
//        //查询周期记录
//        List<Map> cycleList = orderService.redPacketCycle(orderDate, cycle);
//        Map cycleRecord = cycleList.get(0);
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
//        try {
//            //查询在周期开始时间和结束时间期间生成的订单信息
//            List<Order> orderList = orderService.cycleOrder(sdf.parse(cycleRecord.get("startDate").toString()), sdf.parse(cycleRecord.get("endDate").toString()));
//            //周期内是否已经生成最大金额红包
//            boolean biggestRedPacket = false;
//            for(Order order : orderList){
//                if(order.getRandomRedPacket() > 5){
//                    biggestRedPacket = true;
//                    break;
//                }
//            }
//            //是否满足生成最大红包的时间要求
//            boolean isTime = false;
//            Calendar ca = Calendar.getInstance();
//            ca.setTime(orderDate);
//            int redPacketDate = Integer.valueOf(cycleRecord.get("redPacketDate").toString());
//            if(cycle.equals(CYCLE_MONTH)){
//                int dayOfMonth = ca.get(Calendar.DATE);
//                if(dayOfMonth == redPacketDate){
//                    isTime = true;
//                }
//            } else {
//                int dayOfWeek = ca.get(Calendar.DAY_OF_WEEK);
//                if(dayOfWeek == redPacketDate){
//                    isTime = true;
//                }
//            }
//            if(isTime && !biggestRedPacket) isBiggestRedPacket = true;
//        } catch (Exception e){
//            e.printStackTrace();
//        }
//        Map<String,Object> map = new HashMap<>();
//        map.put("isBiggestRedPacket",isBiggestRedPacket);
//        if(isBiggestRedPacket){
//            map.put("randomRedPacket",cycleRecord.get("randomRedPacket"));
//        } else {
//            map.put("randomRedPacket",0.0);
//        }
//        return map;
//
//    }

    /**
     * 获取周期内生成最大红包的号数(生成记录时使用)
     */
    public Integer getCycle(Integer maxDay){
        //获取最大红包生成周期
//        daijiaApi.isRandomRedPacket("cycle").toString();
        String cycle = "month";
        Random random = new Random();
        Integer returnInt = random.nextInt(maxDay);
//        //周期为月
//        if(cycle.equals(CYCLE_MONTH)){
//            Calendar calendar = Calendar.getInstance();
//            calendar.set(Calendar.DATE,1);
//            calendar.roll(Calendar.DATE,-1);
//            //计算一个月最大天数
//            int maxDays = calendar.get(Calendar.DATE);
//            returnInt = random.nextInt(maxDays);
//        }
//        if(cycle.equals(CYCLE_WEEK)){
//            returnInt = random.nextInt(7);
//        }
        return returnInt;
    }

    /**
     * 统计某时间段内生成的订单信息
     * @param startDate 开始日期
     * @param endDate 截止日期
     */
    public List<Map> weekRedPacket(Date startDate, Date endDate){
        List<Order> orderList = orderService.cycleOrder(startDate, endDate);
        List<Map> redPacketList = new ArrayList<>();
        if(null != orderList){
            for(Order order : orderList){
                Map<String, Object> map = new HashMap<>();
                map.put("passenger",order.getPassengerName());
                map.put("passengerPhone",order.getPassengerPhone());
                map.put("orderNo",order.getOrderNo());
                map.put("randomRedPacket",null == order.getRandomRedPacket() ? 0.0 : order.getRandomRedPacket());
                redPacketList.add(map);
            }
        }
        return redPacketList;
    }

    /**
     * 红包生成器
     * 生成[x,y] x到y的随机小数
     */
//    public Double redPacketProducer(){
//        Double randomRedPacket = null;
//        Map<String,Object> map = isBiggestRedPacket(new Date());
//        //生成最大红包
//        if(Boolean.valueOf(map.get("isBiggestRedPacket").toString())){
//            randomRedPacket = Double.valueOf(map.get("randomRedPacket").toString());
//        } else {
//            Double[] section = sectionSelector();
//            Double x = section[0];
//            Double y = section[1];
//            Double d = y - Math.random() * (y - x);
//            DecimalFormat df = new DecimalFormat("0.00");
//            randomRedPacket = Double.valueOf(df.format(d));
//        }
//        return randomRedPacket;
//    }

    /**
     * 生成一个(0.00,1.00]的随机数,获取金额梯度
     * @return
     */
    public static Double[] sectionSelector(){
        Double d = Math.random();
        Double[] section = null;
        if(0.0 < d && d <= 0.8){    //80%
            section = sectionOne;
        }
        else if(0.8 < d && d <= 0.94){  //14%
            section = sectionTwo;
        }
        else if(0.94 < d && d <= 0.97){ //3%
            section = sectionThree;
        }
        else if(0.97 < d && d <= 0.99){ //2%
            section = sectionFour;
        }
        else {  //1%
            section = sectionFive;
        }
        return section;
    }

}
